/*********************************************************
 *********************************************************
 ********************                  *******************
 *************                                ************
 *******                  _oo0oo_                  *******
 ***                     o8888888o                     ***
 *                       88" . "88                       *
 *                       (| -_- |)                       *
 *                       0\  =  /0                       *
 *                     ___/`---'\___                     *
 *                   .' \\|     |// '.                   *
 *                  / \\|||  :  |||// \                  *
 *                 / _||||| -:- |||||- \                 *
 *                |   | \\\  -  /// |   |                *
 *                | \_|  ''\---/''  |_/ |                *
 *                \  .-\__  '-'  ___/-. /                *
 *              ___'. .'  /--.--\  `. .'___              *
 *           ."" '<  `.___\_<|>_/___.' >' "".            *
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |          *
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /          *
 *      =====`-.____`.___ \_____/___.-`___.-'=====       *
 *                        `=---='                        *
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      *
 *********__佛祖保佑__永无BUG__验收通过__钞票多多__*********
 *********************************************************/
package org.jbase.cms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.mapper.BaseMapper;
import org.jbase.cms.dao.CmsSiteConfigDao;
import org.jbase.cms.entity.CmsSiteConfig;
import org.jbase.cms.ext.beetl.CmsFunctionPackage;
import org.jbase.common.service.EntityMapperService;
import org.jbase.common.utils.Pager;
import org.jbase.common.utils.message.Message;
import org.jbase.common.utils.message.MessageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project: jbase-springboot <br/>
 * File: CmsSiteConfigService.java <br/>
 * Class: org.jbase.admin.service.CmsSiteConfigService <br/>
 * Description: CMS站点信息设置. <br/>
 * Copyright: Copyright (c) 2016 <br/>
 * Company: http://git.oschina.net/liuzp315/Jbase <br/>
 * Makedate: 2017-01-07 12:04:40 <br/>
 * gen by Jbase Coder <br/>
 * 
 * @author liuzhanhong
 * @version 1.0
 * @since 1.0
 */
@Service
public class CmsSiteConfigService extends EntityMapperService<CmsSiteConfig> {

	@Autowired
	private CmsSiteConfigDao cmsSiteConfigDao;
	@Autowired
	private CmsFunctionPackage cmsFunctionPackage;

	@Override
	public BaseMapper<CmsSiteConfig> dao() {
		return cmsSiteConfigDao;
	}

	public Pager query(Map<String, String> map, Pager pager) {

		PageQuery<Object> pageQuery = new PageQuery<Object>(pager.getPageNumber(), map, -1, pager.getPageSize());
		cmsSiteConfigDao.selectPager(pageQuery);

		pager.setList(pageQuery.getList());
		pager.setRecordCount((int) pageQuery.getTotalRow());
		return pager;
	}

	public CmsSiteConfig fetchBySid(String sid) {
		return cmsSiteConfigDao.fetchBySid(sid);
	}

	public void deleteBySid(String sid) {
		cmsSiteConfigDao.deleteBySid(sid);
	}

	public Message<List<CmsSiteConfig>> listBySids(String sids) {
		Message<List<CmsSiteConfig>> mess = new Message<>();
		mess.SetMessageCode(MessageCode.SUCCESS);
		mess.setSuccess(true);

		if (StringUtils.isBlank(sids)) {
			return mess;
		}

		if (sids.startsWith(",")) {
			sids = sids.substring(1);
		}

		List<String> sqltemp = new ArrayList<>();
		String[] split = sids.split(",");
		for (String str : split) {
			if (StringUtils.isNotBlank(str))
				sqltemp.add(str);
		}

		Map<String, Object> param = new HashMap<>();
		param.put("sids", sqltemp);
		List<CmsSiteConfig> listBySids = cmsSiteConfigDao.listBySids(param);

		mess.setData(listBySids);

		return mess;
	}

	//////// - 分割线，以上代码为自动生成 - ////////

	/**
	 * 描述 : <描述函数实现的功能>. <br/>
	 * <p>
	 * 
	 * @return
	 */
	public CmsSiteConfig fetchFirst() {
		List<CmsSiteConfig> all = cmsSiteConfigDao.all();
		if (all == null || all.isEmpty())
			return new CmsSiteConfig();
		else
			return all.get(0);
	}

	@Override
	public Message<CmsSiteConfig> saveOrUpdate(Map<String, Object> map) {
		Message<CmsSiteConfig> saveOrUpdate = super.saveOrUpdate(map);
		cmsFunctionPackage.remave(CmsFunctionPackage.SITE_CONFIG);
		return saveOrUpdate;
	}
}
